package fun.wlfj.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import fun.wlfj.dao.IProductDao;
import fun.wlfj.dbc.IConnectionManager;
import fun.wlfj.dbc.MySQLConnection;
import fun.wlfj.vo.Product;

public class ProductDaoImpl extends AbsDaoImpl implements IProductDao {

	public ProductDaoImpl(Connection conn) {
		super(conn);
	}

	@Override
	public List<Product> findByShortName(String name) {
		List<Product> list = new LinkedList<Product>();
		try {
			PreparedStatement pstsm = conn.prepareStatement("select * from Product where ProductShortName = ?;");
			pstsm.setString(1, name);
			ResultSet rs = pstsm.executeQuery();
			while(rs.next()) {
				list.add(new Product(rs.getInt(1), rs.getInt(4), rs.getString(2), rs.getString(3), rs.getInt(5)));
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public String addProduct(Product p) {
		return 
				doProduct(p, "insert into Product(ProductName, ProductShortName, ProductNums, ProductPrice) values(?,?,?,?);")
				.equals("ok") ? "添加成功" : "添加失败";
	}

	/**
	 * 注意！不要随意修改价格 会导致账单信息错乱
	 * @param p
	 * @return
	 */
	@Override
	public String modifyProduct(Product p) {
		return 
				doProduct(p, "update Product set ProductName = ?, ProductShortName = ?, ProductNums = ?, ProductPrice = ? where ProductID = " + p.getProductID())
				.equals("ok") ? "更新成功" : "更新失败";
	}
	
	private String doProduct(Product p, String sql) {
		System.out.println(sql);
		String msg = null;
		try {
			PreparedStatement pstsm = conn.prepareStatement(sql);
			pstsm.setString(1, p.getProductName());
			pstsm.setString(2, p.getProductShortName());
			pstsm.setInt(3, p.getProductNums());
			pstsm.setDouble(4, p.getProductPrice());
			if(pstsm.executeUpdate() == 1) {
				msg = "ok";
			}else {
				msg = "err";
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return msg;
	}
	
	public static void main(String[] args) {
		
		/**
		 * findByShortName
		 * addProduct
		 * modifyProduct
		 */
		
		IConnectionManager manager = new MySQLConnection();
		IProductDao test = new ProductDaoImpl(manager.getConn());
		List<Product> list = test.findByShortName("hsjc");
		System.out.println("下面是通过搜索得到的信息：");
		for(Product product : list)
			System.out.println(product);
		test.addProduct(new Product(10, "血常规", "xcg", 100));
		Product p = test.findByShortName("xcg").get(0);
		System.out.println("创建的项目：" + p.toString());
		p.setProductNums(500);
		test.modifyProduct(p);
		
	}

	@Override
	public List<Product> getProducts() {
		List<Product> list = new LinkedList<Product>();
		try {
			PreparedStatement pstsm = conn.prepareStatement("select * from Product;");
			ResultSet rs = pstsm.executeQuery();
			while(rs.next()) {
				list.add(new Product(rs.getInt(1), rs.getInt(4), rs.getString(2), rs.getString(3), rs.getInt(5)));
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Product getProductByID(int ProductID) {
		Product product = null;
		try {
			PreparedStatement pstsm = conn.prepareStatement("select * from Product where ProductID = ?;");
			pstsm.setInt(1, ProductID);
			ResultSet rs = pstsm.executeQuery();
			if(rs.next()) {
				product = new Product(rs.getInt("ProductID"), rs.getInt("ProductNums"), rs.getString("ProductName"), rs.getString("ProductShortName"), rs.getDouble("ProductPrice"));
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return product;
	}

}
